package com.sjh.zlock;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.utils.CloseableUtils;


/**
 * zookeeper 连接端
 **/
@Slf4j
@Data
public class ZKclient {


    private CuratorFramework client;

    //Zk集群地址
    private static final String ZK_ADDRESS = "106.14.118.168:2181";
    private static final String USER_NAME = "root";
    private static final String PASSWORD = "sjh88888888";
//    private static final String ZK_ADDRESS = "172.31.213.12:2181,172.31.213.13:2181,172.31.213.14:2181";

    public static ZKclient instance = null;

    static {
        instance = new ZKclient();
        instance.init();
    }

    private ZKclient() {

    }

    public void init() {

        if (null != client) {
            return;
        }
        //创建客户端
        if (USER_NAME != null && PASSWORD != null) {
            client = ClientFactory.create(ZK_ADDRESS, USER_NAME, PASSWORD);
        } else {
            client = ClientFactory.createSimple(ZK_ADDRESS);
        }
        //启动客户端实例,连接服务器
        client.start();
    }

    public void destroy() {
        CloseableUtils.closeQuietly(client);
    }




}
